package com.unidt.www.module.expert.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.unidt.www.framework.common.pojo.PageResult;
import com.unidt.www.framework.common.util.object.ConvertUtil;
import com.unidt.www.module.expert.domain.ExpertInvitationDo;
import com.unidt.www.module.expert.domain.ExpertInvitationRuleDo;
import com.unidt.www.module.expert.domain.ExpertRuleUserDo;
import com.unidt.www.module.expert.form.ExpertInvitationQueryForm;
import com.unidt.www.module.expert.mapper.ExpertInvitationMapper;
import com.unidt.www.module.expert.mapper.ExpertInvitationRuleMapper;
import com.unidt.www.module.expert.mapper.ExpertRuleUserMapper;
import com.unidt.www.module.expert.service.IExpertInvitationService;
import com.unidt.www.module.expert.vo.ExpertInvitationVo;
import com.unidt.www.module.expert.vo.ExpertRuleUserVo;
import com.unidt.www.module.expert.vo.ExpertUserVo;
import com.unidt.www.module.expert.vo.InvitationTimeInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author sk
 * @Description:
 * @Date 2022/12/20 15:27
 **/
@Service
public class ExpertInvitationServiceImpl implements IExpertInvitationService {

    @Autowired
    ExpertInvitationMapper expertInvitationMapper;

    @Autowired
    ExpertInvitationRuleMapper expertInvitationRuleMapper;

    @Autowired
    ExpertRuleUserMapper expertRuleUserMapper;

    @Override
    public PageResult<ExpertInvitationDo> pageQuery(ExpertInvitationQueryForm queryForm) {
        LambdaQueryWrapper<ExpertInvitationDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExpertInvitationDo::getDeleted,false);
        lambdaQueryWrapper.like(StringUtils.isNotBlank(queryForm.getSubject()),ExpertInvitationDo::getSubject,queryForm.getSubject());
        lambdaQueryWrapper.orderByDesc(ExpertInvitationDo::getCreateTime);

        return this.expertInvitationMapper.selectPage(queryForm,lambdaQueryWrapper);
    }

    @Override
    public ExpertInvitationVo getDetail(Long id) {
        ExpertInvitationDo expertInvitationDo = this.expertInvitationMapper.selectById(id);
        return ConvertUtil.convert(expertInvitationDo,ExpertInvitationVo.class);
    }

    @Override
    public List<ExpertRuleUserVo> getDynamic(Long invitationId) {

        List<ExpertInvitationRuleDo> ruleList = this.expertInvitationRuleMapper.getByInvitationId(invitationId);
        List<ExpertRuleUserVo> ruleUserVoList = new ArrayList<>();
        if(!ruleList.isEmpty()){
            ruleList.forEach(r->{
                ExpertRuleUserVo expertRuleUserVo = ConvertUtil.convert(r,ExpertRuleUserVo.class);
                /**
                 * 获取已经发短信的名单
                 */
                List<ExpertRuleUserDo> expertRuleUserList =  this.expertRuleUserMapper.getByRuleIdAndMessageStatus(r.getId());
                List<ExpertUserVo> expertList = ConvertUtil.convertList(expertRuleUserList,ExpertUserVo.class);
                expertRuleUserVo.setExpertList(expertList);
                Long totalCount = this.expertRuleUserMapper.countByRuleId(r.getId());
                expertRuleUserVo.setTotalCount(totalCount);
                ruleUserVoList.add(expertRuleUserVo);
            });
        }
        return ruleUserVoList;
    }

    @Override
    public List<ExpertRuleUserVo> getNameList(Long invitationId) {
        List<ExpertInvitationRuleDo> ruleList = this.expertInvitationRuleMapper.getByInvitationId(invitationId);
        List<ExpertRuleUserVo> ruleUserVoList = new ArrayList<>();
        if(!ruleList.isEmpty()){
            ruleList.forEach(r->{
                ExpertRuleUserVo expertRuleUserVo = ConvertUtil.convert(r,ExpertRuleUserVo.class);
                /**
                 * 获取已经发短信的名单
                 */
                List<ExpertRuleUserDo> expertRuleUserList =  this.expertRuleUserMapper.getByRuleIdAndStatus(r.getId(),1);
                List<ExpertUserVo> expertList = ConvertUtil.convertList(expertRuleUserList,ExpertUserVo.class);
                expertRuleUserVo.setExpertList(expertList);
                expertRuleUserVo.setInviteesNumbers(expertList.size());
                Long totalCount = this.expertRuleUserMapper.countByRuleId(r.getId());
                expertRuleUserVo.setTotalCount(totalCount);
                ruleUserVoList.add(expertRuleUserVo);
            });
        }
        return ruleUserVoList;    }

    @Override
    public List<ExpertUserVo> getLeftList(Long ruleId) {
        List<ExpertRuleUserDo> expertRuleUserDos =   this.expertRuleUserMapper.getLeftList(ruleId);
        List<ExpertUserVo> expertUserVos = ConvertUtil.convertList(expertRuleUserDos,ExpertUserVo.class);
        return expertUserVos;
    }

    @Override
    public InvitationTimeInfoVo getInvitationTimeInfo(Long id) {
        InvitationTimeInfoVo invitationTimeInfoVo = new
                InvitationTimeInfoVo();
        invitationTimeInfoVo.setId(id);

        ExpertInvitationDo expertInvitationDo =
                this.expertInvitationMapper.selectById(id);

        Date startTime = expertInvitationDo.getStartTime();

        /**
         * 天倒计时
         */
        if(startTime.after(new Date())){
            invitationTimeInfoVo.setDaysLeft(DateUtil.betweenDay(new Date(),startTime,false));
        }else {
            invitationTimeInfoVo.setDaysLeft(0L);
        }

        /**
         * 专家总数
         */
        Long totalExperts = this.expertRuleUserMapper.countByInvitationIdAndStatus(id,1);
        invitationTimeInfoVo.setTotalExperts(totalExperts);
        return invitationTimeInfoVo;
    }
}
